package com.szqhyxj.youxiangju.biz.receipt.receiptdetail;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.receipt.receiptdetail.signature.SignatureActivity;
import com.szqhyxj.youxiangju.biz.receipt.record.ReceiptRecordActicirty;
import com.szqhyxj.youxiangju.biz.receipt.success.ReceiptSuccessActivity;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;
import com.szqhyxj.youxiangju.sys.components.view.popview.GainSMSPopView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by leung on 2018/11/23.
 */

public class ReceiptDetailActivity extends BaseActivity implements ReceiptDetailContract.View, NavigationBar.NavigationBarListener {

    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.amount)
    EditText amount;
    @BindView(R.id.handlingfee)
    TextView handlingfee;
    @BindView(R.id.billingcard)
    TextView billingcard;
    @BindView(R.id.startReceipt)
    TextView startReceipt;

    ReceiptDetailPresenter presenter;
    String cardo;
    String cardPhone;
    private static final String CHANNELCODE = "channelCode";
    private static final String TRADECARDID = "tradeCardId";
    private static final String TRADETYPE = "type";
    private static final String MINAMOUNT = "minAmount";
    private static final String MAXAMOUNT = "maxAmout";
    private static final String STARTTIME = "startTime";
    private static final String ENDTIME = "endTime";
    String channelCode;
    String tradeCardId;
    String tradeType;
    String minAmount;
    String maxAmount;
    int startTime;
    int endTime;
    @BindView(R.id.fee)
    TextView fee;
    @BindView(R.id.amountrange)
    TextView amountrange;
    @BindView(R.id.timerange)
    TextView timerange;





    public static void start(Activity activity, String bankname, String cardno, String amount, String cardPhone, String channelCode, String tradeCardId, String type, String minAmount, String maxAmount,int starttime,int endtime) {
        Intent intent = new Intent();
        intent.setClass(activity, ReceiptDetailActivity.class);
        intent.putExtra("bankname", bankname);
        intent.putExtra("cardno", cardno);
        intent.putExtra("amount", amount);
        intent.putExtra("cardPhone", cardPhone);
        intent.putExtra(CHANNELCODE, channelCode);
        intent.putExtra(TRADECARDID, tradeCardId);
        intent.putExtra(TRADETYPE, type);
        intent.putExtra(MINAMOUNT, minAmount);
        intent.putExtra(MAXAMOUNT, maxAmount);
        intent.putExtra(STARTTIME,starttime);
        intent.putExtra(ENDTIME,endtime);
        activity.startActivity(intent);

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receipt_detail);
        ButterKnife.bind(this);
        navigationBar.setNavigationBarListener(this);
        cardo = getIntent().getStringExtra("cardno");
        String bankcardnosub = cardo.substring(cardo.length() - 4, cardo.length());
        String str = "(" + bankcardnosub + ")";
        String bankname = getIntent().getStringExtra("bankname");
        navigationBar.setNaviTitle(bankname + str);
        presenter = new ReceiptDetailPresenter(this);
        String money = getIntent().getStringExtra("amount");
        cardPhone = getIntent().getStringExtra("cardPhone");
        presenter.init();
        channelCode = getIntent().getStringExtra(CHANNELCODE);
        tradeCardId = getIntent().getStringExtra(TRADECARDID);
        tradeType = getIntent().getStringExtra(TRADETYPE);
        minAmount = getIntent().getStringExtra(MINAMOUNT);
        maxAmount = getIntent().getStringExtra(MAXAMOUNT);
        startTime = getIntent().getIntExtra(STARTTIME,0);
        endTime = getIntent().getIntExtra(ENDTIME,0);
        if (tradeType.equals("1")) {
            fee.setText("费率：0.79%+单笔1.0元");
            amountrange.setText("单笔最低100元，单笔最高3万元");
            timerange.setText("10:30-19:50 随时收款，2小时内到账");
        }
        if (tradeType.equals("2")) {
            fee.setText("费率：0.65%+单笔2元");
            amountrange.setText("单笔最低"+minAmount+"元，单笔最高"+maxAmount+"元");
            String start = getTime(startTime);
            String end = getTime(endTime);
            timerange.setText(start+"-"+end+" 随时收款，2小时内到账");
        }
        amount.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                        s = s.toString().subSequence(0, s.toString().indexOf(".") + 3);
                        amount.setText(s);
                        amount.setSelection(s.length());
                    }
                } //如果.在起始位置,则起始位置自动补0
                if (s.toString().trim().substring(0).equals(".")) {
                    s = "0" + s;
                    amount.setText(s);
                    amount.setSelection(2);
                }
                //如果起始位置为0并且第二位跟的不是".",则无法后续输入
                if (s.toString().startsWith("0") && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        amount.setText(s.subSequence(0, 1));
                        amount.setSelection(1);
                        return;
                    }
                }
                if (tradeType.equals("1")) {
                    presenter.setAmount(s.toString(), money, tradeType, "100", "30000");
                }
                if (tradeType.equals("2")) {
                    presenter.setAmount(s.toString(), money, tradeType, minAmount, maxAmount);
                }
            }
        });


    }
    private String getTime(int time) {
        String lastTime ="";

        if (time>10){
            lastTime = time+":00";
        }else{
            lastTime = "0"+time+":00";
        }
        return lastTime;

    }
    GainSMSPopView gainSMSPopView;

    @OnClick(R.id.startReceipt)
    public void onClick() {
        if (tradeType.equals("1")) {
            if (amount.getText().toString() == null || amount.getText().toString().equals("")) {
                Toast.makeText(this, "请填写金额！", Toast.LENGTH_LONG).show();
            } else {
                SignatureActivity.start(this, cardo, amount.getText().toString(), cardPhone);
            }
        }
        if (tradeType.equals("2")) {
            if (amount.getText().toString() == null || amount.getText().toString().equals("")) {
                Toast.makeText(this, "请填写金额！", Toast.LENGTH_LONG).show();
            } else {
                // SignatureActivity.start(this, cardo, amount.getText().toString(), cardPhone);
                gainSMSPopView = new GainSMSPopView(ReceiptDetailActivity.this, cardPhone,"已发送验证码至");
                gainSMSPopView.setOnClickListener(new GainSMSPopView.OnClickListener() {
                    @Override
                    public void onClickConfirm(String code) {
                        if (code.equals("")) {
                            Toast.makeText(ReceiptDetailActivity.this, "验证码不能为空！", Toast.LENGTH_LONG).show();
                        } else {
                            presenter.receiptConfirm(channelCode, tradeCardId, "-1", amount.getText().toString(), code);
                        }
                    }

                    @Override
                    public void onClickgetMsg() {
                        presenter.onOnlineGetSms(channelCode, tradeCardId, amount.getText().toString());
                    }
                });
                gainSMSPopView.show();
            }
        }


    }

    public abstract static class SimpleTextWatcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }

    @Override
    public void showCard(String card) {
        billingcard.setText(card);
    }

    @Override
    public void showHandingFee(String fee) {
        handlingfee.setText(fee);
    }

    @Override
    public void showEnClickable() {
        startReceipt.setEnabled(false);
        startReceipt.setBackgroundResource(R.drawable.background_btn_grey04);
    }

    @Override
    public void showClickable() {
        startReceipt.setEnabled(true);
        startReceipt.setBackgroundResource(R.drawable.background_btn04);
    }

    @Override
    public void onlineSuccess() {
        gainSMSPopView.dismiss();
       // ReceiptSuccessActivity.start(this, false);
        ReceiptRecordActicirty.start(this,"online");
    }

    @Override
    public void enshowDialog() {
        gainSMSPopView.dismiss();
    }

    @Override
    public void navigationLeft() {
        finish();
    }

    @Override
    public void navigationRight() {

    }
}
